package com.leyou.listener;

import com.leyou.user.service.GoodsHtmlService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class GoodsListener {
    @Autowired
    GoodsHtmlService goodsHtmlService;

    @RabbitListener(bindings = @QueueBinding(
            value =@Queue(value = "LEYOU.ITEM.SAVE.QUEUE",durable = "true"),
            exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions = "true", type = ExchangeTypes.TOPIC),
            key= {"item.insert","item.update"}
    ))
    public void save(Long spuId){
        if (spuId == null)
            return;
        else goodsHtmlService.createHtml(spuId);
    }

    @RabbitListener(bindings = @QueueBinding(
            value =@Queue(value = "LEYOU.ITEM.DELETE.QUEUE",durable = "true"),
            exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key= {"item.delete"}
    ))
    public void delete(Long spuId){
        File file = new File("D:\\nginx\\nginx-1.14.0\\html\\item\\" + spuId + ".html");
        file.deleteOnExit();//存在则删除
    }



}
